import {} from 'react'
import TableTags from '../tableTags'
import { CaseSchema } from '../schema/caseSchema'
import { Dispatch } from 'redux'
import { addWindow } from '@redux/actions/tab'
import { useDispatch } from 'react-redux'

const outsideList = ['港', '澳', '台', '外国']

// 获取标签
function getTags (d: CaseSchema, dispatch: Dispatch) {
  const showTags: string[] = []
  const noticeMap: { [k: string]: { title: string, clickHandle?: () => void } } = {}
  const partyAreas = [...(d.applicants || []), ...(d.respondents || [])].map(li => li.area)
  const hasOut = partyAreas.some(li => outsideList.indexOf(li) !== -1)
  if (d.delay_day) {
    showTags.push('缓')
  }
  if (d.serieno) {
    showTags.push('系')
    noticeMap['系'] = {
      title: `系列号：${d.serieno}`,
      clickHandle: () => toSeriesCase(d, dispatch)
    }
  }
  if (hasOut || d.foreign_related_type) {
    showTags.push('外')
    noticeMap['外'] = {
      title: d.foreign_related_type || '主体涉外'
    }
  }
  if (d.casetype === '金融案件') {
    showTags.push('金')
    noticeMap['金'] = {
      title: d.casesubtype
    }
  }
  if (d.counter_caseno_desc) {
    showTags.push('反')
    noticeMap['反'] = {
      title: `反请求案号：${d.counter_caseno_desc}`
    }
  }
  return {
    showTags,
    noticeMap
  }
}

// 前往系列案列表
function toSeriesCase (data: CaseSchema, dispatch: Dispatch) {
  const d = data.serienoInfo
  dispatch(addWindow({ id: `seriesCaseDetail-${d.id}`, title: `系列案-${d.serieno}`, componentName: 'SeriesCaseDetail', forceRefresh: false, meta: { id: d.id, editable: false } }))
}

interface Props {
  data: CaseSchema
}

function Main ({ data: d }: Props) {
  const dispatch = useDispatch()
  const caseno = d.caseno_desc || d.case_no || d.acceptno_desc || d.acceptno
  const { showTags, noticeMap } = getTags(d, dispatch)
  return (
    <span>
      <TableTags tags={['缓', '系', '金', '外', '反']} showTags={showTags} noticeMap={noticeMap} />
      <span>{caseno}</span>
    </span>
  )
}

export default Main
